# -*- coding: utf-8 -*-
# @Time   : 2023/9/13 17:20
# @Author : 大龙 🚀
# @File   : gansu_illegal.py

import re

from apps.tax_illegal.tax_illegal.items import NetTaxIllegalProItem
from apps.tax_illegal.tax_illegal.spiders import BaseTaxIllegalSpider
from utils.tools import urlencode, parse_url_params


class QingdaoIllegalSpider(BaseTaxIllegalSpider):
    name = "qingdao_illegal"
    province: str = "青岛"
    url: str = "http://qingdao.chinatax.gov.cn/resourcedata/sswf2019/"

    def start_requests(self):
        yield from self.etax_search(**{"etax_url": "https://etax.qingdao.chinatax.gov.cn:8443"})
        url = "http://qingdao.chinatax.gov.cn/search/was5/web/search?page=1&channelid=223090&perpage=10&outlinepage=10&prepage=10&templet=SearchResult_2020sswf.jsp"
        yield self.Request(url, callback=self.parse_list)

    def parse_list(self, response, **kwargs):
        page = kwargs.get("page", 1)
        yield from self.parse(response, **kwargs)
        page_total_num = response.xpath(".").re_first(r"共\s*(\d+)\s*条")
        root_url, params = parse_url_params(response.request.url)
        if page_total_num:
            if int(page_total_num) > page * 10:
                page = page + 1
                yield self.Request(
                    root_url + "?" + urlencode(params={**params, "page": f"{page}"}),
                    callback=self.parse_list,
                    cb_kwargs={"page": page},
                )

    def parse(self, response, **kwargs):
        li_list = response.xpath("//table/tbody/tr")
        for li in li_list:
            url = li.xpath(".//a/@href").get()
            yield self.Request(url, callback=self.parse_detail)

    def parse_detail(self, response, **kwargs):
        item = NetTaxIllegalProItem()
        item.province = self.province

        info = []
        for i in response.xpath('//table[@class="sswf_table"]//tr'):
            res = [i.xpath("string(.)").get().strip() for i in i.xpath(".//td")]
            if res:
                if len(res) != 2:
                    continue
                info.append(res)

        for k, *v in info:
            v = " ".join(v)
            k = re.sub(r"[^\u4e00-\u9fa5]+", "", k)
            if "纳税人名称" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.company_name = v
            if "纳税人识别号" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.taxpayer_id = v
            if "组织机构代码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.org_code = v
            if "注册地址" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.city = item.resolve_register_address(v)[0]
                item.country = item.resolve_register_address(v)[1]
                item.address = v
            if "法定代表人或负责人姓名性别及身份证号码或其他证件号码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.legal_representative = v
            if "违法期间法人代表或者负责人姓名、性别及身份证号码或其他证件号码" in k:
                v = re.sub(r"(<!--.*?-->|<.*?>|\s*)", "", v, flags=re.S)
                item.illegal_legal_representative = v
            if "负有直接责任的财务人员姓名性别及身份证号码或其他证件号码" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.resp_financial = v
            if "实际负责人姓名性别及身份证号码或其他证件号码" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.resp_person = v
            if "负有直接责任的中介机构信息" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split()).replace("null", "")
                item.resp_intermediary = v
            if "案件性质" in k:
                item.illegal_status = v
            if "主要违法事实" in k:
                v = re.sub(r"(<!--.*?-->|\s*|<.*?>)", "", v, flags=re.S)
                illegal_facts_src = re.sub(r"(<.*?>|\s*|“|”)", "", re.sub(r"(<!--.*?-->)", "", v))
                item.illegal_facts = illegal_facts_src
            if "相关法律依据及税务处理处罚情况" in k:
                v = " ".join(re.sub(r"(<!--.*?-->|<.*?>)", "", v, flags=re.S).split())
                item.basis_and_punishment = v

        pub_date = response.xpath('//*[@name="Pubdate"]/@content').get()
        item.year = pub_date[:10].replace("-", "")

        yield item


if __name__ == "__main__":

    from scrapy import cmdline

    cmdline.execute(argv=["scrapy", "crawl", "qingdao_illegal"])
